package com.flying.carclub.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.flying.carclub.BaseActivity;
import com.flying.carclub.R;
import com.flying.carclub.api.ApiImpl;
import com.flying.carclub.bean.UserInfoBean;
import com.flying.carclub.http.OnResponseListener;
import com.flying.carclub.utils.SPUtils;
import com.flying.carclub.utils.StringUtil;
import com.flying.carclub.widget.TitleBar;
import com.stonesun.newssdk.NewsAgent;
import com.stonesun.newssdk.bean.UserInfo;
import com.stonesun.newssdk.itf.AppUserItf;

public class SignupActivity extends BaseActivity {
    private EditText phoneEt;
    private EditText pwdEt;
    private EditText pwd1Et;
    private Button loginBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_signup);
        phoneEt = findViewById(R.id.et_phone);
        pwdEt = findViewById(R.id.et_pwd);
        pwd1Et = findViewById(R.id.et_pwd1);
        loginBtn = findViewById(R.id.bt_login);

        initListener();
    }

    private void initListener() {

        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (TextUtils.isEmpty(phoneEt.getText())) {
                    showToast("请输入手机");
                    return;
                }

                if (!StringUtil.isMobileNumber(phoneEt.getText().toString())) {
                    showToast("请输入11位正确的手机号码");
                    return;
                }
                if (TextUtils.isEmpty(pwdEt.getText()) || pwdEt.getText().length() < 6) {
                    showToast("密码错误");
                    return;
                }
                if (TextUtils.isEmpty(pwd1Et.getText()) || !(pwdEt.getText().toString().equals(pwd1Et.getText().toString()))) {
                    showToast("两次密码输入不一致");
                    return;
                }
                new ApiImpl().httpSignup(phoneEt.getText().toString(), pwdEt.getText().toString(), new OnResponseListener() {
                    @Override
                    public void onSuccess(Object response) {
                        showToast("注册成功");
                        if (response != null){
                            final UserInfoBean userInfoBean = (UserInfoBean) response;
                            if (userInfoBean != null) {
                                SPUtils.setUserInfo(SignupActivity.this, userInfoBean);

                                AppUserItf appUserItf=new AppUserItf() {
                                    @Override
                                    public boolean isLogin() {
                                        return true;
                                    }

                                    @Override
                                    public UserInfo getUserinfo() {
                                        UserInfo userInfo=new UserInfo();
                                        userInfo.setUid(userInfoBean.authKey);
                                        userInfo.setToken(userInfoBean.authKey);
                                        userInfo.setUsericon("");
                                        userInfo.setUsername(userInfoBean.username);
                                        userInfo.setFrom("QQ");
                                        return userInfo;
                                    }
                                };
                                NewsAgent.SetUserImpForSDK(appUserItf,LoginActivity.class);
                            }
                        }


                        setResult(RESULT_OK);
                        SignupActivity.this.finish();
                    }

                    @Override
                    public void onError(String err) {
                        if (TextUtils.isEmpty(err)){
                            err = "网络请求失败";
                        }
                        showToast(err);
                    }
                });


            }
        });
    }
}

